package Domain.forumProtcol;

import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;

import objects.User;

import com.thoughtworks.xstream.XStream;

import Domain.forumManager.ForumController;
import Domain.stompProtocol.ForumFrame;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;
import Logger.ForumLogger;

public abstract class ProtocolHelper
{

	
	/** The queue prefix. */
	protected String queuePrefix = "";
	protected XStream xstream=new XStream();
	/** The _stomp client. */
	protected StompClient _stompClient;
	protected ForumController forumController;
	protected final char DELIMITER=';';  //will separate the filed in the message at the protocol
	protected ForumFrame forumfrm;
	
	public String getNameOfForum(String str)
	{
		try 
		{
			int index = str.indexOf(DELIMITER)+1;
			int indexend = str.indexOf(DELIMITER, index);
			if(indexend == -1)
				indexend = str.length();
			String tmp = str.substring(index,indexend).trim();
			return tmp;
		} 
		catch (RuntimeException e) 
		{
		} 
		return null;
	}


	public String getNameOfThread (String str)
	{
		try 
		{
			int index = str.indexOf(DELIMITER)+1;
			String tmp = str.substring(index).trim();
			index = tmp.indexOf(DELIMITER)+1;
			int indexend =tmp.indexOf(DELIMITER, index);
			if (indexend == -1)
				indexend = tmp.length();
			tmp = tmp.substring(index,indexend).trim();
			return tmp;
		} 
		catch (RuntimeException e) 
		{
		}
		return null;
	}
	
	public String getTitleOfMessageFromEditRequest(String str)
	{
		int i = str.lastIndexOf(DELIMITER);
		String ans=str.substring(0, i);
		i=ans.lastIndexOf(DELIMITER);
		return ans.substring(i+1).trim();
	}
	
	public String getTitleOfMessage(String str)
	{
		int i=str.lastIndexOf(DELIMITER);
//		String ans=str.substring(0, i);
//		i=ans.lastIndexOf(DELIMITER);
//		return ans.substring(i+1).trim();
		return str.substring(i+1).trim();
	}
	
	public String getUserToRemove(String str)
	{
		int i=str.indexOf("remove user");
		int j=str.indexOf(DELIMITER);
		String ans=str.substring(i+11,j);//str.substring(i+12);
		return ans.trim();
	}
	
	public String getContentsOfMessage(String str)
	{
		
		int i=str.lastIndexOf(DELIMITER);
		return str.substring(i+1).trim();
		
	}

	
	protected String sendSecessMessage(String nameOfUser)
	{
		forumfrm.setSeccessOporation();
		return this.sendPlayerMsg(nameOfUser,forumfrm);
	}
	
	protected String InvalidMessage(String nameOfUser)
	{
		//System.out.println("***********************");
		ForumLogger.write(Level.INFO,"recieved Inviled format of message");
		//System.out.println(" inviled format of message\n");
		forumfrm.setErr(forumfrm.UNKOWNERR);
		return this.sendPlayerMsg(nameOfUser, forumfrm);
	}


	public ForumFrame getForumfrm()
	{
		return forumfrm;
	}
	

	/**
	 * Send player msg.
	 *
	 * @param user the user that send the message
	 * @param msg the msg
	 */
	protected String sendPlayerMsg(String user,String msg)
	{
		
		this._stompClient.sendStompFrame(StompFrame.createSendFrame("fserver",this.queuePrefix + user , msg));
		return msg;
	}
	/**
	 * send the user the frame as xml
	 * @param user the user we want to send the frame
	 * @param f the forumframe to be sent
	 * @return the message that has sent
	 */
	protected String sendPlayerMsg(String user,ForumFrame f)
	{
		this.xstream.alias("ForumFrame", ForumFrame.class);
		String xml=this.xstream.toXML(f);
		
		return this.sendPlayerMsg(user, xml);
		
	}
	private boolean searchForName(Vector<User> u,String name)
	{
		for (Iterator iterator = u.iterator(); iterator.hasNext();)
		{
			User user = (User) iterator.next();
			if (user.getUserName().equals(name))
				return true;
		}
		return false;
	}
	public String sendToFriendAndUsers(String str, String nameOfUser,
			String ans,String title)
	{
		//System.err.println("the nameOfUser is :   "+nameOfUser+"\n is freinds are"
			//	+forumController.getFriendsOfUser(nameOfUser));////remove
		
		
		Vector <User> fv;
		/*
		if (fv!=null)
		{
			for (Iterator <User>iterator = fv.iterator(); iterator
					.hasNext();)
			{
				User user = (User) iterator.next();
				//System.err.println("User :  "+user+"  nameOfUser :  "+nameOfUser+
					//	" getNameOfForum(str):"+getNameOfForum(str)+" getNameOfThread(str)):  "+getNameOfThread(str));
				 this.forumfrm.setFriendPublishedMessage(nameOfUser, getNameOfForum(str), getNameOfThread(str));
				ans+=this.sendPlayerMsg(user.getUserName(),this.forumfrm);
			}
			
		}
		*/
		  ////////////////we won"t send to all user but only if friend post a message
		fv=this.forumController.getAllUsers();
		if (fv!=null)
		{
			for (Iterator<User> iterator = fv.iterator(); iterator
					.hasNext();)
			{
				User user = (User) iterator.next();
				if(user.getUserName()!=nameOfUser)
				{
					this.forumfrm.setUserPublishedMessage(nameOfUser, getNameOfForum(str), getNameOfThread(str),title);
					ans+=this.sendPlayerMsg(user.getUserName(),this.forumfrm);
					if(searchForName(forumController.getFriendsOfUser(user.getUserName()),nameOfUser))
							{
								this.forumfrm.setFriendPublishedMessage(nameOfUser, getNameOfForum(str), getNameOfThread(str),title);
								ans+=this.sendPlayerMsg(user.getUserName(),this.forumfrm);
							}
				}
			}
			
		}
		
		this.sendSecessMessage(nameOfUser);
		return ans;
	}
	
	
}
